<?php

namespace App\Http\Controllers\scheduleController;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

/**
 * 暂时没有使用到，后续提高数据相应时间可以使用
 */
class ScheduleController extends Controller
{
    private $taskName = 'daily-task-at-10-00';

    /**
     * 停止定时任务
     */
    public function stopTask(Request $request)
    {
        // 设置任务锁定，有效期为24小时
        Cache::put('schedule-'.$this->taskName, true, now()->addHours(24));

        Log::info('定时任务已手动停止', ['task' => $this->taskName]);

        return response()->json([
            'status' => 'success',
            'message' => '定时任务已停止',
            'task' => $this->taskName,
            'is_locked' => true,
        ]);
    }

    /**
     * 恢复定时任务
     */
    public function resumeTask(Request $request)
    {
        // 清除任务锁定
        Cache::forget('schedule-'.$this->taskName);

        Log::info('定时任务已恢复', ['task' => $this->taskName]);

        return response()->json([
            'status' => 'success',
            'message' => '定时任务已恢复',
            'task' => $this->taskName,
            'is_locked' => false,
        ]);
    }

    /**
     * 检查任务状态
     */
    public function checkTaskStatus(Request $request)
    {
        $isLocked = Cache::has('schedule-'.$this->taskName);

        return response()->json([
            'status' => 'success',
            'task' => $this->taskName,
            'is_locked' => $isLocked,
            'is_running' => ! $isLocked,
        ]);
    }
}
